<template>
  <div class="wrap">
    <div class="titles">查询</div>
    <div class="sel">
      <div class="txt">关键词</div>
      <input type="text" placeholder="合伙人名称/手机号" v-model="params.keyWord">
      <div class="btn" style="cursor: pointer;" @click="getParManList()">搜索</div>
      <div v-if="roleList.indexOf('增加合伙人') != '-1'" class="addpar" @click="addParMan()">
        <img class="add" src="../../images/main/add.png" alt> 增加合伙人
      </div>
    </div>
    <div class="clear"></div>
    <div class="titles">全部</div>
    <el-table :data="userList" stripe style="width: 100%;">
      <el-table-column align="center" prop="partner_name" label="合伙人/企业名称" width="180"></el-table-column>
      <el-table-column align="center" prop="partner_account" label="合伙人账号" width="180"></el-table-column>
      <el-table-column align="center" prop="partner_phone" label="手机号"></el-table-column>
      <el-table-column align="center" prop="region" label="经营区域"></el-table-column>
      <el-table-column align="center" prop="commission" label="平台佣金"></el-table-column>
      <el-table-column align="center" label="加盟期限">
        <template slot-scope="scope">{{scope.row.join_age ? scope.row.join_age + '年' : '-'}}</template>
      </el-table-column>
      <el-table-column width="200" align="center" label="更新时间">
        <template slot-scope="scope">
          <div>{{setDate(scope.row.time) +' '+setTime(scope.row.time)}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="当前状态">
        <template slot-scope="scope">
          <div>{{(scope.row.states == 0) ? '正常' : '冻结中'}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <div>
            <el-popover placement="right" width="200" trigger="click">
              <div class="menu-list">
                <div v-if="roleList.indexOf('分佣设置') != '-1' && roleList.indexOf('合伙人管理') != '-1'">
                  <el-button
                    @click="subCommission(scope.row.id)"
                    type="primary"
                    size="mini"
                    round
                  >分佣设置</el-button>
                </div>
                <div v-if="roleList.indexOf('编辑合伙人') != '-1'">
                  <el-button
                    @click="editParMan(true,scope.row.id)"
                    type="primary"
                    size="mini"
                    round
                  >编辑</el-button>
                </div>
                <div v-if="roleList.indexOf('编辑合伙人') != '-1'">
                  <el-button @click="resetPwd( scope.row.id )" type="primary" size="mini" round>重置密码</el-button>
                </div>
                <div v-if="roleList.indexOf('合伙人管理') != '-1'">
                  <el-button
                    @click="partnerDeatils(scope.row.id)"
                    type="primary"
                    size="mini"
                    round
                  >详情</el-button>
                </div>
                <div v-if="roleList.indexOf('收款账号') != '-1'">
                  <el-button @click="accParMan(scope.row.id)" type="primary" size="mini" round>收款账号</el-button>
                </div>
                <div
                  v-if="roleList.indexOf('冻结/解除冻结') != '-1' && roleList.indexOf('合伙人管理') != '-1'"
                >
                  <el-button
                    @click="setFrozen(scope.row.states,scope.row.id)"
                    type="primary"
                    size="mini"
                    round
                  >{{scope.row.states == 0 ? '冻结' : '解除冻结'}}</el-button>
                </div>
              </div>
              <div slot="reference" class="show-menu">···</div>
            </el-popover>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-show="userList && userList.length > 0"
      :background="true"
      @current-change="handleCurrentChange"
      :current-page="params.page"
      layout="total, prev, pager, next, jumper"
      :total="params.total"
      style="text-align:center;margin:20px auto;"
    ></el-pagination>
  </div>
</template>

<script>
import Global from "../tool/Global.vue";
import axios from "axios";
import qs from "qs"; // 将请求转换成form 表单提交形式
export default {
  data() {
    return {
      loading: false,
      params: {
        page: 1,
        limit: 10,
        keyWord: "",
        total: 10
      },
      userList: [],
      roleList: ""
    };
  },
  methods: {
    /**
     *  获取权限
     */
    getRole: function() {
      let that = this;
      axios({
        method: "post",
        url: `${that.$store.state.url}/sysPermission/selectByEmployeeId`,
        data: qs.stringify({
          empId: that.$cookie.get("pid")
        })
      }).then(res => {
        // console.log(res)
        let arr = res.data.data;
        let role = [];
        for (let i = 0; i < arr.length; i++) {
          role.push(arr[i].name);
        }
        // console.log(arr)
        this.$store.commit("UPDATE_NAV_PERMISSION", arr);
        this.roleList = role.toString();
        // this.roleList = this.roleList
      });
    },
    // 重置密码
    resetPwd(id) {
      // this.$prompt(
      //   '输入新密码', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消'
      // }).then(({ value }) => {
      //   console.log ( value );

      // }).catch(() => {

      // });
      var that = this
      this.$confirm("确定重置密码？").then(() => {
        axios
          .post(
            `${this.$store.state.url}/tPartner/updatePwd`,
            qs.stringify({
              id: id
              // pwd: value
            })
          )
          .then(response => {
            if (response.code == 0) {
              this.$store.state.fn.success(response.data.msg);
            this.$store.state.fn.success( '修改成功' ).then (() => {
              _this.$router.back(-1);
            });
            let logData = {
                loginTime:this.$cookie.get("loginTime"),
                account:this.$cookie.get("userAccount"),
                content:'重置合伙人密码',
              operationTime:this.setDate(new Date()) + ' ' +this.setTime(new Date())
              }
              axios({
          method: "post",
          url: `${that.$store.state.url}/log/addLog`,
          data: qs.stringify(logData)
        }).then( (res) => {
          console.log(res,'222')
        })
            } else {
              this.$alert(response.data.msg)
            }
            
          });
      });
    },

    // 分页
    handleCurrentChange: function(index) {
      let that = this;
      that.params.page = index;
      that.getParManList();
    },
    // 增加合伙人
    addParMan() {
      this.$router.push({
        path: "/main/addParMan"
      });
    },
    /**
     * 增加或者修改合伙人
     */
    editParMan: function(bol, id) {
      if (bol) {
        Global.userId = id;
        Global.userEdit = true;
      } else {
        Global.userEdit = false;
      }
      this.$router.push({
        path: "/main/editParMan",
        query: {
          id: id
        }
      });
    },
    /**
     * 修改合伙人账户
     */
    accParMan: function(id) {
      Global.userId = id;
      this.$router.push({
        path: "/main/accParMan",
        query: {
          id: id
        }
      });
    },
    // 分佣设置
    subCommission: function(id) {
      Global.userId = id;
      this.$router.push({
        path: "/main/subCommission"
      });
    },
    //  合伙人详情
    partnerDeatils: function(id) {
      Global.userId = id;
      this.$router.push({
        path: "/main/partnerDetails"
      });
    },
    /**
     * 冻结/解冻
     */
    setFrozen: function(states, id) {
      let that = this;
      if (states == 0) {
        states = 1;
      } else {
        states = 0;
      }

      this.$confirm(states === 0 ? "是否解除冻结?" : "是否确认冻结?").then(
        () => {
          let data = {
            states: states,
            id: id
          };
          axios({
            method: "post",
            url: `${that.$store.state.url}/tPartner/updateStates`,
            data: qs.stringify(data)
          }).then(res => {
            if (res.data.code == 0) {
              this.$store.state.fn.success(
                states === 0 ? "解除冻结成功" : "确认冻结成功"
              );
              that.getParManList();
              this.$store.state.fn.success( '修改成功' ).then (() => {
              _this.$router.back(-1);
            });
            let logData = {
                loginTime:this.$cookie.get("loginTime"),
                account:this.$cookie.get("userAccount"),
                content: states==0 ? '解除冻结合伙人' : '冻结合伙人',
              operationTime:this.setDate(new Date()) + ' ' +this.setTime(new Date())
              }
              axios({
          method: "post",
          url: `${that.$store.state.url}/log/addLog`,
          data: qs.stringify(logData)
        }).then( (res) => {
          console.log(res,'222')
        })
            } else {
              that.$alert(res.msg);
            }
          });
        }
      );
    },
    /**
     * 获取合伙人列表
     */
    getParManList: function() {
      let that = this;
      this.loading = true;
      axios({
        method: "post",
        url: `${that.$store.state.url}/tPartner/getPartnerList`,
        data: qs.stringify(that.params)
      }).then(res => {
        this.loading = false;
        // console.log(res)
        if (res.data.code == 0) {
          that.userList = res.data.data;
          that.params.total = res.data.count;
        } else {
          alert(res.msg);
        }
      });
    },
     /**
     * 根据时间毫秒数 返回年月日
     */
    setDate: function(str) {
      let times = new Date(str);
      return `${times.getFullYear()}-${
        times.getMonth() + 1 < 10
          ? "0" + (times.getMonth() + 1)
          : times.getMonth() + 1
      }-${times.getDate() < 10 ? "0" + times.getDate() : times.getDate()}`;
    },
    /**
     * 根据时间毫秒数 返回时分秒
     */
    setTime: function(str) {
      let times = new Date(str);
      return `${
        times.getHours() < 10 ? "0" + times.getHours() : times.getHours()
      }:${
        times.getMinutes() < 10 ? "0" + times.getMinutes() : times.getMinutes()
      }:${
        times.getSeconds() < 10 ? "0" + times.getSeconds() : times.getSeconds()
      }`;
    }
  },
  mounted() {
    this.getParManList();
    this.getRole();
  }
};
</script>
<style scoped type="text/scss" lang="scss">
.titles {
  height: 62px;
  line-height: 62px;
  color: #778ca2ff;
  font-size: 14px;
  width: 100%;
  display: inline-block;
}
.wrap {
  /* padding: 68px 136px; */
  /* width: 100%; */
  width: 90%;
  margin: 0 auto;
}
.sel {
  height: 32px;
  line-height: 32px;
  padding: 23px 20px;
  background: #fff;
}
.sel .txt {
  float: left;
  font-family: PingFangSC-Medium;
  font-size: 14px;
  color: rgba(37, 38, 49, 1);
  font-weight: bold;
  width: 42px;
}
.sel input {
  float: left;
  width: 232px;
  height: 32px;
  background: rgba(255, 255, 255, 1);
  border-radius: 16px;
  border: 1px solid rgba(232, 236, 239, 1);
  color: #888888;
  margin-left: 17px;
  text-align: center;
  box-sizing: border-box;
  outline: none;
}
.btn {
  float: left;
  color: #fff;
  width: 80px;
  height: 32px;
  background: rgba(75, 116, 255, 1);
  border-radius: 24px;
  border: 1px solid rgba(75, 116, 255, 1);
  text-align: center;
  margin-left: 37px;
}

.addpar {
  float: right;
  font-size: 14px;
  color: #fff;
  cursor: pointer;
  width: 114px;
  height: 32px;
  background: rgba(75, 116, 255, 1);
  border-radius: 24px;
  border: 1px solid rgba(75, 116, 255, 1);
  text-align: center;
  line-height: 32px;
}
.add {
  width: 18px;
  height: 18px;
  float: left;
  margin: 8px;
}
.show-menu {
  color: rgba(75, 116, 255, 1);
  cursor: pointer;
}
.menu-list div {
  margin: 5px;
  float: left;
  box-sizing: border-box;
  width: 40%;
}
</style>
